package com.soft.sitegenerator.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.soft.sitegenerator.Component;
import com.soft.sitegenerator.DataBaseComponent;
import com.soft.sitegenerator.business.hibernate.CompositeTemplate;
import com.soft.sitegenerator.business.hibernate.SimpleTemplate;
import com.soft.sitegenerator.business.hibernate.SitePage;
import com.soft.sitegenerator.business.hibernate.Template;
import com.soft.sitegenerator.business.hibernate.User;
import com.soft.sitegenerator.exceptions.ObjectNotFoundException;
import com.soft.sitegenerator.form.AsignTemplateEditForm;

public class AsignTemplateEditAction extends DispatchAction {

	public ActionForward deleteAsignTemplate(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		Long id = new Long(Long.parseLong(request.getParameter("id")));
		
		Component component = new DataBaseComponent();
		Template template = (SimpleTemplate) component.getBean("simpleTemplatesHib");
		template.setId(id);
		try {
			template.load();
		} catch (ObjectNotFoundException e) {
			template = (CompositeTemplate) component.getBean("compositeTemplatesHib");
			try {
				template.setId(id);
				template.load();
			} catch (ObjectNotFoundException e1) {
				e1.printStackTrace();
			}
		}

		template.delete();

		return mapping.findForward("showList");
	}

	public ActionForward addAsignTemplate(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {

		AsignTemplateEditForm asignTemplateEditForm = (AsignTemplateEditForm) form;

		Component component = new DataBaseComponent();
		User userAdmin = (User) component.getBean("usersHib");
		userAdmin.setId((Long) request.getSession().getAttribute("USER_LOGIN"));
		try {
			userAdmin.load();
		} catch (ObjectNotFoundException e) {
			e.printStackTrace();
			return mapping.findForward("bad");
		}
		asignTemplateEditForm.setWebSites(userAdmin.getMyWebSites());

		Template template = (Template) component.getBean("templatesHib");
		asignTemplateEditForm.setTemplates(template.findAllNotModificable());

		User user = (User) component.getBean("usersHib");
		asignTemplateEditForm.setUsers(user.findAll());

		return mapping.findForward("showAdd");
	}

	public ActionForward editAsignTemplate(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		AsignTemplateEditForm asignTemplateEditForm = (AsignTemplateEditForm) form;

		Long id = new Long(Long.parseLong(request.getParameter("id")));

		Template asignTemplate = asignTemplateEditForm.getAsignTemplate();
		asignTemplate.setId(id);

		try {
			asignTemplate.load();
		} catch (ObjectNotFoundException e) {
			e.printStackTrace();
		}

		asignTemplateEditForm.setIdUsers(asignTemplate.getUserComplete().getId());
		asignTemplateEditForm.setIdTemplate(asignTemplate.getId());
		asignTemplateEditForm.setIdSitePages(asignTemplate.getSitePage().getId());

		Component component = new DataBaseComponent();
		User userAdmin = (User) component.getBean("usersHib");
		userAdmin.setId((Long) request.getSession().getAttribute("USER_LOGIN"));
		try {
			userAdmin.load();
		} catch (ObjectNotFoundException e) {
			e.printStackTrace();
			return mapping.findForward("bad");
		}
		asignTemplateEditForm.setWebSites(userAdmin.getMyWebSites());

		Template template = (Template) component.getBean("templatesHib");
		asignTemplateEditForm.setTemplates(template.findAllNotModificable());

		User user = (User) component.getBean("usersHib");
		asignTemplateEditForm.setUsers(user.findAll());

		return mapping.findForward("showEdit");
	}

	public ActionForward saveAsignTemplate(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		AsignTemplateEditForm asignTemplateEditForm = (AsignTemplateEditForm) form;

		Component component = new DataBaseComponent();
		SitePage sitePage = (SitePage) component.getBean("sitePagesHib");

		sitePage.setId(asignTemplateEditForm.getIdSitePages());

		try {
			sitePage.load();
		} catch (ObjectNotFoundException e) {
			e.printStackTrace();
		}

		User user = (User) component.getBean("usersHib");

		user.setId(asignTemplateEditForm.getIdUsers());

		try {
			user.load();
		} catch (ObjectNotFoundException e) {
			e.printStackTrace();
		}

		Template template = (SimpleTemplate) component.getBean("simpleTemplatesHib");
		template.setId(asignTemplateEditForm.getIdTemplate());
		try {
			template.load();
		} catch (ObjectNotFoundException e) {
			template = (CompositeTemplate) component.getBean("compositeTemplatesHib");
			try {
				template.setId(asignTemplateEditForm.getIdTemplate());
				template.load();
			} catch (ObjectNotFoundException e1) {
				e1.printStackTrace();
			}
		}

		template.setSitePage(sitePage);
		template.setUserComplete(user);
		template.setCanUpdate("si");
		template.setCompositeTemplate(null);
		// hago un clone
		template.clonar();

		return mapping.findForward("showList");

	}

	public ActionForward updateAsignTemplate(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		AsignTemplateEditForm asignTemplateEditForm = (AsignTemplateEditForm) form;

		Component component = new DataBaseComponent();
		SitePage sitePage = (SitePage) component.getBean("sitePagesHib");

		sitePage.setId(asignTemplateEditForm.getIdSitePages());

		try {
			sitePage.load();
		} catch (ObjectNotFoundException e) {
			e.printStackTrace();
		}

		User user = (User) component.getBean("usersHib");

		user.setId(asignTemplateEditForm.getIdUsers());

		try {
			user.load();
		} catch (ObjectNotFoundException e) {
			e.printStackTrace();
		}

		Template template = (Template) component.getBean("templatesHib");

		template.setId(asignTemplateEditForm.getIdTemplate());

		try {
			template.load();
		} catch (ObjectNotFoundException e) {
			e.printStackTrace();
		}
		template.setSitePage(sitePage);
		template.setUserComplete(user);
		template.update();

		return mapping.findForward("showList");

	}

}